package com.dingshi.security.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dingshi.security.R;
import com.dingshi.security.RepairCreateActivity.ItemClickListener;
import com.dingshi.security.entity.DeviceInfo;
import com.dingshi.security.util.Utils;

public class DevicePopupWindow extends PopupWindow implements OnClickListener {

	private LayoutInflater inflater;
	private Context context;
	private TextView la_text;
	private TextView log_text;
	private ListView listview;
	private ItemClickListener itemClickListener;
	private ArrayList<DeviceInfo> dataList;
	
	public DevicePopupWindow(Context context) {
		super(context);
		this.context = context;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.device_popup, null);
		viewGroup.setBackgroundColor(Color.parseColor("#fbfbfb"));
		
		log_text = (TextView) viewGroup.findViewById(R.id.textView1);
		la_text = (TextView) viewGroup.findViewById(R.id.textView2);
		listview = (ListView) viewGroup.findViewById(R.id.listview);
		listview.setCacheColorHint(Color.TRANSPARENT);
		listview.addHeaderView(createHeaderView());
		
		

		setContentView(viewGroup);
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		viewGroup.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				dismiss();
				return false;
			}
		});
	}

	public void updateUI(Location location, ArrayList<DeviceInfo> list) {
		this.dataList = list;
		log_text.setText("当前经度：" + (location!=null?location.getLongitude():""));
		la_text.setText("当前纬度：" + (location!=null?location.getLatitude():""));
		ListAdapter adapter = new ListAdapter(context, dataList);
		listview.setAdapter(adapter);
	}
	
	private View createHeaderView() {
		LinearLayout line = new LinearLayout(context);
		line.setOrientation(LinearLayout.HORIZONTAL);
		AbsListView.LayoutParams LP = new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		line.setLayoutParams(LP);
		LinearLayout.LayoutParams deviceLP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		deviceLP.weight = 1;
		deviceLP.gravity = Gravity.CENTER_VERTICAL;
		TextView deviceNum = new TextView(context);
		deviceNum.setGravity(Gravity.CENTER_HORIZONTAL);
		deviceNum.setText("设备编号");
		deviceNum.setTextSize(18.f);
		deviceNum.setTextColor(Color.parseColor("#009ce1"));
		line.addView(deviceNum,deviceLP);
		
		
		LinearLayout.LayoutParams statusLP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		statusLP.weight = 0.5f;
		statusLP.gravity = Gravity.CENTER_VERTICAL;
		TextView status = new TextView(context);
		status.setGravity(Gravity.CENTER_HORIZONTAL);
		status.setText("设备状态");
		status.setTextSize(18.f);
		status.setTextColor(Color.parseColor("#009ce1"));
		line.addView(status,statusLP);
		
		
		LinearLayout.LayoutParams locationLP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		locationLP.weight = 1.5f;
		locationLP.gravity = Gravity.CENTER_VERTICAL;
		TextView location = new TextView(context);
		location.setText("经纬度");
		location.setGravity(Gravity.CENTER_HORIZONTAL);
		location.setTextSize(18.f);
		location.setTextColor(Color.parseColor("#009ce1"));
		line.addView(location,locationLP);
		return line;
	}


	public void show(View parent) {
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int[] loc = new int[2];
		parent.getLocationInWindow(loc);
		int offX=(loc[0]+parent.getWidth()/2)-Utils.getScreenWidth(context)/2;
		int offY=loc[1] + parent.getHeight();
		params.setMargins(offX, 0, 0, offY);
		super.showAtLocation(parent, 0, offX, offY);
	}


	@Override
	public void onClick(View v) {
		dismiss();
	}

	final class ListAdapter extends BaseAdapter {

		private Context context;
		private ArrayList<DeviceInfo> list;
		private LayoutInflater inflater;
		public ListAdapter(Context context,ArrayList<DeviceInfo> deviceList) {
			this.context = context;
			this.list = deviceList;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		}
		
		@Override
		public int getCount() {
			return list!=null?list.size():0;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			DeviceInfo deviceInfo = list.get(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.list_item, null);
				holder = new ViewHolder();
				holder.item = (LinearLayout)convertView.findViewById(R.id.list_item);
				holder.device = (TextView) convertView.findViewById(R.id.textView1);
				holder.status = (TextView) convertView.findViewById(R.id.textView2);
				holder.location = (TextView) convertView.findViewById(R.id.textView3);
				holder.item.setId(position);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.item.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if(itemClickListener!=null) {
						Log.e("position", "position is " + arg0.getId());
						itemClickListener.onItemClickListener(dataList.get(arg0.getId()).getN());
					}
					dismiss();
				}
			});
			updateData(holder, deviceInfo);
			return convertView;
		}
		private void updateData(ViewHolder holder, DeviceInfo deviceInfo) {
			holder.device.setText(deviceInfo!=null?deviceInfo.getI():"未知设备");
			holder.status.setText(deviceInfo!=null?deviceInfo.getS():"未知");
			holder.location.setText(deviceInfo!=null?deviceInfo.getLn()+"/"+deviceInfo.getLa():"未知位置");
		}
		final class ViewHolder {
			public LinearLayout item;
			public TextView location;
			public TextView status;
			public TextView device;
		}
	}

	public void setOnItemClickListener(ItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
		
	}
}
